/**
 *对Date的扩展，将 Date 转化为指定格式的String
 *月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
 *年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
 *例子：
 *(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
 *(new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
 */
Date.prototype.format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}
//获取日期周开始日期（周一为每周的开始,周日为每周的结束）
export function beginOfWeek(dateValue){
  let date;
  if (dateValue instanceof Date) {
    date = dateValue;
  } else {
    date = new Date(dateValue);
  }
  let subDay = 0;
  let weekDay = date.getDay();
  if (weekDay == 0) {
    //周天
    subDay = 6;
  } else {
    subDay = weekDay - 1;
  }
  let beginDateTime = date.getTime() - (86400000 * subDay);
  return new Date(beginDateTime);

}

//获取日期周结束日期（周一为每周的开始,周日为每周的结束）
export function endOfWeek(dateValue){
  let date;
  if (dateValue instanceof Date) {
    date = dateValue;
  } else {
    date = new Date(dateValue);
  }
  let weekDay = date.getDay();
  if (weekDay == 0) {
    //周天
    return date;
  } else {
    let addDay = 7 - weekDay;
    let endDateTime = date.getTime() + (86400000 * addDay);
    return new Date(endDateTime);
  }

}
// 日期转为字符串yyyy-MM-dd
export function  toDateStr(date){
  let dateStr= date.format("MM月dd日");
  return dateStr;
}
// 日期转为字符串yyyy-MM-dd HH:mm:ss
export function  toDateTimeStr(date){
  let dateStr= date.format("yyyy-MM-dd hh:mm:ss");
  return dateStr;
}

//日期加减法天
export function dateAddDay(date,days) {
  let result=new Date();
  result.setDate(date.getDate()+days);
  return result;
}
//获取日期上周开始日期
export function lastBeginOfWeek(dateValue){
     let date= dateAddDay(dateValue,-7);
     let beginDate=beginOfWeek(date)
     return beginDate;
}

//获取日期上周开始日期
export function lastEndOfWeek(dateValue){
    let date= dateAddDay(dateValue,-7);
    let endDate=endOfWeek(date)
    return endDate;
}

//获取当前月的第一天
export function beginOfMonth(){
  let y = new Date().getFullYear(); //获取年份
  let m = new Date().getMonth() + 1; //获取月份
  let d = '01'
  m = m < 10 ? '0' + m : m; //月份补 0

  //return [y,m,d].join("-")
  return m+"月"+d+"日"
}

//获取当前月的最后一天
export function endOfMonth(){
  let y = new Date().getFullYear(); //获取年份
  let m = new Date().getMonth() + 1; //获取月份
  let d = new Date(y, m, 0).getDate(); //获取当月最后一日
  m = m < 10 ? '0' + m : m; //月份补 0
  d = d < 10 ? '0' + d : d; //日数补 0

  //return [y,m,d].join("-")
  return m+"月"+d+"日"

}

//获取上月的第一天
export function lastBeginOfMonth(){
  let now =new Date();
  let date= dateAddDay(now,-30);
  let y = date.getFullYear(); //获取年份
  let m = date.getMonth() + 1; //获取月份
  let d = '01'
  m = m < 10 ? '0' + m : m; //月份补 0

  //return [y,m,d].join("-")
  return m+"月"+d+"日"
}

//获取上月的最后一天
export function lastEndOfMonth(){
  let now =new Date();
  let date= dateAddDay(now,-30);
  let y = date.getFullYear(); //获取年份
  let m = date.getMonth() + 1; //获取月份
  let d = new Date(y, m, 0).getDate(); //获取当月最后一日
  m = m < 10 ? '0' + m : m; //月份补 0
  d = d < 10 ? '0' + d : d; //日数补 0

  //return [y,m,d].join("-")
  return m+"月"+d+"日"

}


//获取今年
export function thisYear(){
  let now =new Date();
  let y = now.getFullYear(); //获取年份
  return y+"年"
}

//获取去年
export function lastYear(){
  let now =new Date();
  let y = now.getFullYear()-1; //获取年份
  return y+"年"

}

// 获取今天 yyyy-MM-dd
export function  getToday(){
  let now =new Date();
  let dateStr= now.format("yyyy-MM-dd");
  return dateStr;
}

// 获取明天yyyy-MM-dd
export function  getTomorrow(){
  let now =new Date();
  let tomorrow=dateAddDay(now,1)
  let dateStr= tomorrow.format("yyyy-MM-dd");
  return dateStr;
}


